/**
 * 
 */
package com.snl.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import org.springframework.transaction.annotation.Transactional;

import com.snl.model.Advertisement;
import com.snl.model.Party;
import com.snl.model.User;
import com.snl.service.MainService;

/**
 * @author Bryan Liu
 *
 */
@Transactional
public class MainServiceImpl implements MainService {

	protected EntityManager em;
	
	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	/* (non-Javadoc)
	 * @see com.snl.service.MainService#addUser()
	 */
	@Override
	public void addUser(User user) {
		em.persist(user);
	}

	/* (non-Javadoc)
	 * @see com.snl.service.MainService#userLogin()
	 */
	@Override
	public boolean userLogin() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.snl.service.MainService#listAds()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Advertisement> listAds() {
		
		Query query = em.createQuery("select a from Advertisement a order by a.id");
		
		return query.getResultList();
		
	}

	/* (non-Javadoc)
	 * @see com.snl.service.MainService#listComingPartys()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Party> listComingPartys(Integer startRecord) {
		Date currentDate = new Date();
		
		if(startRecord==null){
			startRecord = 0;
		}
		
		Query query = em.createQuery("select p from Party p where p.startDate >=:currentDate order by p.startDate desc");
		
		query.setParameter("currentDate", currentDate, TemporalType.DATE);
		query.setFirstResult(startRecord);
		query.setMaxResults(7);
		
		
		return query.getResultList();
		
	}
	
	@SuppressWarnings("unchecked")
	public Integer getTotalComingEventCount(){
		Date currentDate = new Date();
		
		Query query = em.createQuery("select count(p) from Party p where p.startDate >=:currentDate order by p.startDate desc");
		query.setParameter("currentDate", currentDate, TemporalType.DATE);
		
	    List result = query.getResultList();
	    
	    if(result!=null && result.size()>0){
	    	return ((Long) result.get(0)).intValue();
	    }else return 0;
	    
		
	}
	
	@SuppressWarnings("unchecked")
	public Integer getTotalPastEventCount(){
		Date currentDate = new Date();
		
		Query query = em.createQuery("select count(p) from Party p where p.startDate <:currentDate order by p.startDate desc");
		query.setParameter("currentDate", currentDate, TemporalType.DATE);
		
	    List result = query.getResultList();
	    
	    if(result!=null && result.size()>0){
	    	return ((Long) result.get(0)).intValue();
	    }else return 0;
		
	}

	/* (non-Javadoc)
	 * @see com.snl.service.MainService#listPastPartys()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Party> listPastPartys(Integer startRecord) {
		
		if(startRecord==null){
			startRecord = 0;
		}
		
		Date currentDate = new Date();
		
		Query query = em.createQuery("select p from Party p where p.startDate <:currentDate order by p.startDate desc");
		
		query.setParameter("currentDate", currentDate, TemporalType.DATE);
		query.setFirstResult(startRecord);
		query.setMaxResults(7);
		
		
		return query.getResultList();
		
	}
	
	/**
	 * Find the advertisement object by ad id;
	 * @param id
	 * @return
	 */
	public Advertisement getAdvertisement(Integer id){
		
		return em.find(Advertisement.class, id);
		
	}
	
}
